package main

import "fmt"

var m = map[string]string{
	"name":   "小虾米",
	"site":   "首席执行官",
	"course": "go语言体系导学",
}

func main() {

	fmt.Println("======= map定义 =======")
	definition()
	fmt.Println("======= map的遍历 =======")
	for k, v := range m {
		fmt.Println(k, " => ", v)
	}
	fmt.Println("======= map的操作 =======")
	//访问不存在的key，返回 zero value
	fmt.Println("m[\"courses\"] = ", m["courses"])
	fmt.Println("m[\"course\"] = ", m["course"])
	v, ok := m["courses"]
	fmt.Printf("v = %v, ok = %v\n", v, ok)

	fmt.Println("======= map的删除 =======")
	delete(m, "courses")
	fmt.Println("======= map的key必须可以比较相等，除了slice，func，map的内建类型都可以作为key =======")
	s1 := "ababcabcd"

	fmt.Println(findLongString(s1))
}

func findLongString(s string) int {
	//find := ""
	lastOccurred := make(map[byte]int)
	start := 0
	maxLength := 0
	//遍历字符串
	for i, ch := range []byte(s) {
		if lastI, ok := lastOccurred[ch]; ok && lastI >= start {
			start = lastOccurred[ch] + 1
		}
		if i-start+1 > maxLength {
			maxLength = i - start + 1
		}
		//更新字符最后一次出现的位置
		lastOccurred[ch] = i
		fmt.Println(ch, start, lastOccurred, maxLength)
	}
	return maxLength
}

func definition() {
	m := map[string]string{
		"name": "小虾米",
		"site": "首席执行官",
	}
	fmt.Println(m)
	m2 := make(map[string]int)
	var m3 map[string]int
	fmt.Println(m2, m3)
}
